﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
namespace KabiManagementWeb.Models
{
    public enum StatisticsTime { day, week, month, year }
    public class StatisticsFacade
    {

        private KBManagementDataContext context;
        public StatisticsFacade(DBUtils.DBType type)
        {
            context = DBUtils.GetContext(type);
        }
        public StatisticsFacade()
        {

        }
        public StatisticsFacade(KBManagementDataContext context)
        {
            this.context = context;
        }
        public List<Store> GetStore()
        {
            return context.Stores.ToList();
        }
        public List<Store_Log> GetStoreLog()
        {
            return context.Store_Logs.ToList();
        }
        public List<Store_Log> GetStoreLogByType(bool type)
        {
            return context.Store_Logs.Where(x => x.TypeIE == type).ToList();
        }
        
        public Statistic GetStatistics(StatisticsTime type)
        {
            var sta = new Statistic();
            switch (type)
            {
                case StatisticsTime.day: DayStatistics(sta);
                    break;
                case StatisticsTime.week: WeekStatistics(sta);
                    break;
                case StatisticsTime.month: MonthStatistics(sta);
                    break;
                case StatisticsTime.year: YearStatistics(sta);
                    break;
                default:
                    break;
            }
            return sta;
        }

        private void YearStatistics(Statistic sta)
        {
            if (context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Year == DateTime.Now.Year).Sum(x => x.Amount).HasValue)
            {
                sta.TotalFoodAmount = context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Year == DateTime.Now.Year).Sum(x => x.Amount).Value;
            }
            if (context.Store_Logs.Where(x => x.TransferDate.HasValue && x.TransferDate.Value.Year == DateTime.Now.Year).Sum(x => x.Price).HasValue)
                sta.TotalImport = context.Store_Logs.Where(x => x.TransferDate.Value.Year == DateTime.Now.Year).Sum(x => x.Price).Value;
            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Year == DateTime.Now.Year).Sum(x => x.Total_Amount).HasValue)
                sta.TotalAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Year == DateTime.Now.Year).Sum(x => x.Total_Amount).Value;
            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Year == DateTime.Now.Year).Sum(x => x.Purchase_Amount).HasValue)
                sta.TotalProductAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Year == DateTime.Now.Year).Sum(x => x.Purchase_Amount).Value;
        }

        private void MonthStatistics(Statistic sta)
        {

            if (context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Month == DateTime.Now.Month).Sum(x => x.Amount).HasValue)
            {
                sta.TotalFoodAmount = context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Month == DateTime.Now.Month).Sum(x => x.Amount).Value;
            }

            if (context.Store_Logs.Where(x => x.TransferDate.HasValue && x.TransferDate.Value.Month == DateTime.Now.Month).Sum(x => x.Price).HasValue)
                sta.TotalImport = context.Store_Logs.Where(x => x.TransferDate.Value.Month == DateTime.Now.Month).Sum(x => x.Price).Value;

            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Month == DateTime.Now.Month).Sum(x => x.Total_Amount).HasValue)
                sta.TotalAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Month == DateTime.Now.Month).Sum(x => x.Total_Amount).Value;

            if (context.ExtraFees.Where(x => x.EF_Month.HasValue && x.EF_Month == DateTime.Now.Month && x.EF_Year.Value == DateTime.Now.Year).Sum<ExtraFee>(x => x.EF_MONEY) >= 0)
            {
                sta.TotalExport = context.ExtraFees.Where(x => x.EF_Month == DateTime.Now.Month && x.EF_Year == DateTime.Now.Year).Sum<ExtraFee>(x => x.EF_MONEY);
            }
            sta.TotalSpend += sta.TotalExport;

            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Month == DateTime.Now.Month).Sum(x => x.Purchase_Amount).HasValue)
                sta.TotalProductAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Month == DateTime.Now.Month).Sum(x => x.Purchase_Amount).Value;
        }

        private void WeekStatistics(Statistic sta)
        {
            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            if (context.Order_Details.ToList().Where(x => x.Purchase_Order.Purchase_Date.HasValue &&
                ciCurr.Calendar.GetWeekOfYear(x.Purchase_Order.Purchase_Date.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Amount).HasValue)
            {
                sta.TotalFoodAmount = context.Order_Details.ToList().Where(x => x.Purchase_Order.Purchase_Date.HasValue &&
                    ciCurr.Calendar.GetWeekOfYear(x.Purchase_Order.Purchase_Date.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Amount).Value;
            }
            if (context.Store_Logs.ToList().Where(x => x.TransferDate.HasValue &&
                ciCurr.Calendar.GetWeekOfYear(x.TransferDate.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Price).HasValue)
                sta.TotalImport = context.Store_Logs.ToList().Where(x => ciCurr.Calendar.GetWeekOfYear(x.TransferDate.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Price).Value;
            if (context.Purchase_Orders.ToList().Where(x => x.Purchase_Date.HasValue &&
                ciCurr.Calendar.GetWeekOfYear(x.Purchase_Date.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Total_Amount).HasValue)
                sta.TotalAmount = context.Purchase_Orders.ToList().Where(x => x.Purchase_Date.HasValue &&
                    ciCurr.Calendar.GetWeekOfYear(x.Purchase_Date.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Total_Amount).Value;
            if (context.Purchase_Orders.ToList().Where(x => x.Purchase_Date.HasValue &&
                  ciCurr.Calendar.GetWeekOfYear(x.Purchase_Date.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Purchase_Amount).HasValue)
                sta.TotalProductAmount = context.Purchase_Orders.ToList().Where(x => x.Purchase_Date.HasValue &&
                    ciCurr.Calendar.GetWeekOfYear(x.Purchase_Date.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)).Sum(x => x.Purchase_Amount).Value;
        }

        private void DayStatistics(Statistic sta)
        {
            if (context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Date == DateTime.Now.Date).Sum(x => x.Amount).HasValue)
            {
                sta.TotalFoodAmount = context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Date == DateTime.Now.Date).Sum(x => x.Amount).Value;
            }

            if (context.Store_Logs.Where(x => x.TransferDate.HasValue && x.TransferDate.Value.Date == DateTime.Now.Date).Sum(x => x.Price).HasValue)
                sta.TotalImport = context.Store_Logs.Where(x => x.TransferDate.Value.Date == DateTime.Now.Date).Sum(x => x.Price).Value;
            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == DateTime.Now.Date).Sum(x => x.Total_Amount).HasValue)
                sta.TotalAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == DateTime.Now.Date).Sum(x => x.Total_Amount).Value;
            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == DateTime.Now.Date).Sum(x => x.Purchase_Amount).HasValue)
                sta.TotalProductAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == DateTime.Now.Date).Sum(x => x.Purchase_Amount).Value;
        }

        public void ViewDayStatistics(Statistic sta,DateTime taget)
        {
            if (context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Date == taget.Date).Sum(x => x.Amount).HasValue)
            {
                sta.TotalFoodAmount = context.Order_Details.Where(x => x.Purchase_Order.Purchase_Date.HasValue && x.Purchase_Order.Purchase_Date.Value.Date == taget.Date).Sum(x => x.Amount).Value;
            }

            if (context.Store_Logs.Where(x => x.TransferDate.HasValue && x.TransferDate.Value.Date == taget.Date).Sum(x => x.Price).HasValue)
                sta.TotalImport = context.Store_Logs.Where(x => x.TransferDate.Value.Date == taget.Date).Sum(x => x.Price).Value;

            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == taget.Date).Sum(x => x.Total_Amount).HasValue)
                sta.TotalAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == taget.Date).Sum(x => x.Total_Amount).Value;

            if (context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == taget.Date).Sum(x => x.Purchase_Amount).HasValue)
                sta.TotalProductAmount = context.Purchase_Orders.Where(x => x.Purchase_Date.HasValue && x.Purchase_Date.Value.Date == taget.Date).Sum(x => x.Purchase_Amount).Value;
        }
    }
}